package top.lyc829.zzl_android.ui.regist;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import java.io.IOException;

import okhttp3.Response;
import top.lyc829.zzl_android.logic.bean.ResultBean;
import top.lyc829.zzl_android.logic.network.Network;
import top.lyc829.zzl_android.logic.util.FormatUtil;
import top.lyc829.zzl_android.logic.util.HttpUtil;
import top.lyc829.zzl_android.logic.util.Utils;
import top.lyc829.zzl_android.logic.util.gson.JsonUtilNew;

public class RegistViewModel extends ViewModel {

    private MutableLiveData<String> verifyCodeLiveData;

    public MutableLiveData<String> getVerifyCodeLiveData() {
        if (verifyCodeLiveData == null) {
            verifyCodeLiveData = new MutableLiveData<>();
        }
        return verifyCodeLiveData;
    }

    public void sendCode(String email) {
        Network.sendVerifyCode(email, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                String resStr = res.body().string();

                ResultBean<String> resultBean = JsonUtilNew.fromJsonObject(resStr, String.class);
                if (resultBean.getCode().equals(200)) {
                    getVerifyCodeLiveData().postValue(resultBean.getData());
                } else {

                    failed(new IOException(resultBean.getMsg()));
                }

            }

            @Override
            public void failed(IOException e) {
                Utils.Sout(e.getMessage());
                getVerifyCodeLiveData().postValue("-1");
            }
        });
    }
}
